package com.pcbly.web.servlet;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.captcha.CaptchaServiceProxy;

import com.octo.captcha.service.CaptchaService;
import com.octo.captcha.service.CaptchaServiceException;

public class JCaptchaServiceProxyImpl implements CaptchaServiceProxy {
	protected static final Log log = LogFactory
			.getLog(JCaptchaServiceProxyImpl.class);
	private CaptchaService jcaptchaService;

	@Override
	public boolean validateReponseForId(String id, Object response) {
		// TODO Auto-generated method stub
		log.debug("validating captcha response");
		try {
			boolean isHuman = jcaptchaService.validateResponseForID(id,
					response).booleanValue();
			if (isHuman) {
				System.out.println("captcha passed");
				log.debug("captcha passed");
			} else {
				log.warn("captcha failed");
				System.out.println("captcha failed");
			}
			return isHuman;

		} catch (CaptchaServiceException cse) {
			// fixes known bug in JCaptcha
			log.error("captcha validation failed due to exception", cse);
			// System.out.println("captcha validation failed due to exception"+cse.getMessage());
			return false;
		}
	}

	public void setJcaptchaService(CaptchaService jcaptchaService) {
		this.jcaptchaService = jcaptchaService;
	}

}
